<%@ Control Language="C#" ClassName="CommentsControll" %>
<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>
<%@ Register Namespace="System.Collections.Generic" Tagprefix="g" %>

<%@ Import Namespace="Grr.Web" %>
<%@ Import Namespace="Grr.Model" %>


<script runat="server">

    private static readonly int MaxPageSize = 5;
    
    private int CommentPage
    {
        get { 
            object o = ViewState["CommentPage"];
            if (o == null)
                return 0;
            return (int)o;
        }
        set { 
            ViewState["CommentPage"] = value; 
        }
    }

    private long GrurrahId
    {
        get
        {
            object o = ViewState["GrurrahId"];
            if (o == null)
                return 0;
            return (long)o;
        }
        set
        {
            ViewState["GrurrahId"] = value;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Grurrah g = (Grurrah) Session ["CurrentGrurrah"];

            if (g != null)
            {
                GrurrahId = g.Id;
                CommentPage = 0;
                
                UpdateGrid();
            }
        }
    }

    private void UpdateGrid ()
    {
        int page = CommentPage;

        if (page <= 0)
        {
            NewerLinkButton.Visible = false;

            if (page < 0)
            {
                CommentPage = 0;
                return;
            }
        } else {
            NewerLinkButton.Visible = true;
        }

        // Pull one more comment than needed so we know whether we should
        // display the 'older' link.  The extra comment is removed from the list
        // in the IF block below.
        List<Comment> comments = Comment.GetCommentsForGrurrah(GrurrahId, CommentPage * MaxPageSize, MaxPageSize + 1);

        if (comments.Count < MaxPageSize + 1)
        {
            OlderLinkButton.Visible = false;
        }
        else
        {
            comments.RemoveAt(MaxPageSize);
            OlderLinkButton.Visible = true;
        }
        
        GridView1.DataSource = comments;
        GridView1.DataBind();

        GridPanel.ForceAnUpdate();
    }

    protected void OlderLinkButton_Click(object sender, EventArgs e)
    {
        CommentPage++;
        UpdateGrid();
    }

    protected void NewerLinkButton_Click(object sender, EventArgs e)
    {
        CommentPage--;
        UpdateGrid();
    }

    protected void CancelCommentLinkButton1_Click(object sender, EventArgs e)
    {
        HideAddCommentPanel();
    }

    protected void SubmitCommentLinkButton_Click(object sender, EventArgs e)
    {
        GrurrahUser user = Session["CurrentUser"] as GrurrahUser;
        if (user == null)
        {
            RaiseBubbleEvent(this, new UserNotificationEventArgs("Sorry, you must be logged in to comment on Grurrahs."));
            return;
        }
        
        Comment.AddComment(user.Name, GrurrahId, AddCommentTextBox.Text);
        HideAddCommentPanel();
        CommentPage = 0;
        UpdateGrid();
    }

    private void ShowAddCommentPanel()
    {
        AddCommentLinkButton.Visible = false;
        AddCommentPanel.Visible = true;
    }
    
    private void HideAddCommentPanel()
    {
        AddCommentLinkButton.Visible = true;
        AddCommentPanel.Visible = false;
        AddCommentTextBox.Text = String.Empty;
    }

    protected void AddCommentLinkButton_Click(object sender, EventArgs e)
    {
        ShowAddCommentPanel();
    }

    protected string FormatPicture(Comment comment)
    {
        return String.Concat("http://media.grurrah.com/users-icons/", comment.Picture);
    }
</script>

<gaia:Label ID="Title" runat="server" style="font-weight: bolder; font-size: x-large;">Comments</gaia:Label>
<hr />
<gaia:LinkButton ID="AddCommentLinkButton" Text="[Add a comment]" runat="server" OnClick="AddCommentLinkButton_Click"></gaia:LinkButton>
<gaia:Panel ID="AddCommentPanel" Visible="false" runat="server">
    <table>
        <tr>
            <td><gaia:TextBox ID="AddCommentTextBox" runat="server" TextMode="MultiLine" Width="400"></gaia:TextBox></td>
        </tr>
        <tr>
            <td align="right">
                <gaia:LinkButton ID="CancelCommentLinkButton1" Text="Cancel" runat="server" OnClick="CancelCommentLinkButton1_Click"></gaia:LinkButton>
                |
                <gaia:LinkButton ID="SubmitCommentLinkButton" Text="Add Comment" runat="server" OnClick="SubmitCommentLinkButton_Click"></gaia:LinkButton>
            </td>
        </tr>
    </table>
    
    
</gaia:Panel>

<gaia:Panel ID="GridPanel" runat="server">
    <asp:GridView ID="GridView1" BorderStyle="None" GridLines="None" CellPadding="5" AutoGenerateColumns="False" runat="server">
        <Columns>
            <asp:TemplateField ItemStyle-VerticalAlign="Top">
                <ItemTemplate>
                    <center>
                        <asp:Image ID="Image1" runat="server" ImageUrl="<%#  FormatPicture ((Comment) Container.DataItem) %>" />
                        <br />
                        <%#  ((Comment) Container.DataItem).FriendlyUserName %>
                    </center>
                </ItemTemplate>
            </asp:TemplateField>
            
            <asp:TemplateField ItemStyle-VerticalAlign="Top">
                <ItemTemplate>
                    <%# ((Comment) Container.DataItem).Text %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</gaia:Panel>

<gaia:Panel ID="PagerPanel" runat="server">
    <table>
        <tr>
            <td align="left"><gaia:LinkButton ID="NewerLinkButton" Text="<< Newer" Visible="false" runat="server" OnClick="NewerLinkButton_Click"></gaia:LinkButton></td>
            <td align="right"><gaia:LinkButton ID="OlderLinkButton" Text="Older >>" runat="server" OnClick="OlderLinkButton_Click"></gaia:LinkButton></td>
        </tr>
    </table>
</gaia:Panel>

       